﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Super.Common.Strings
{
    public class StringPlus
    {
        public static List<string> GetStrArray(string str, char speater, bool toLower)
        {
            List<string> list = new List<string>();
            string[] ss = str.Split(speater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != speater.ToString())
                {
                    string strVal = s;
                    if (toLower)
                    {
                        strVal = s.ToLower();
                    }
                    list.Add(strVal);
                }
            }
            return list;
        }

        /// <summary>
        /// 不重复的随机数
        /// </summary>
        /// <returns></returns>
        public static string GetRandNum(int num)
        {
            int x = 1000;
            int y = 9999;

            switch (num)
            {
                case 1:
                case 2:
                    x = 10;
                    y = 99;
                    break;
                case 3:
                    x = 100;
                    y = 999;
                    break;
                case 4:
                    x = 1000;
                    y = 9999;
                    break;
                case 5:
                    x = 10000;
                    y = 99999;
                    break;
                case 6:
                    x = 100000;
                    y = 999999;
                    break;
                case 7:
                    x = 1000000;
                    y = 9999999;
                    break;
                case 8:
                    x = 10000000;
                    y = 99999999;
                    break;
            }


            string ranName = string.Empty;
            Random random = new Random(Guid.NewGuid().GetHashCode());
            //ranName += DateTime.Now.ToString("yyyyMMddHHmmss");
            ranName += random.Next(x, y);
            return ranName;
        }

        /// <summary>
        /// 精确截取字符串 全角为2，半角为1， 按半角长度截取
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string SubStrByByte(string str, int len)
        {
            if (string.IsNullOrEmpty(str))
                return string.Empty;

            int bytecount = Encoding.Default.GetByteCount(str);
            int sum = 0;
            if (bytecount > len)
            {
                for (int i = 0; i < len; i++)
                {
                    if (sum < len)
                    {
                        sum += Encoding.Default.GetByteCount(str[i].ToString());
                    }
                    else
                    {
                        if (sum % 2 == 0)
                        {
                            sum = i;
                        }
                        else
                        {
                            sum = i - 1;
                        }
                        break;
                    }
                }
                str = str.Substring(0, sum)+"...";
            }
            return str;
        }

        public static string[] GetStrArray(string str)
        {
            return str.Split(new char[',']);
        }
        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i]);
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(speater);
                }
            }
            return sb.ToString();
        }
        #region 删除最后一个字符之后的字符

        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        public static string DelLastComma(string str)
        {
            return str.Substring(0, str.LastIndexOf(","));
        }

        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static string DelLastChar(string str, string strchar)
        {
            return str.Substring(0, str.LastIndexOf(strchar));
        }

        #endregion
        /// <summary>
        /// 转全角的函数(SBC case)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC(string input)
        {
            //半角转全角：
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }
        /// <summary>
        ///  转半角的函数(SBC case)
        /// </summary>
        /// <param name="input">输入</param>
        /// <returns></returns>
        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        public static List<string> GetSubStringList(string o_str, char sepeater)
        {
            List<string> list = new List<string>();
            string[] ss = o_str.Split(sepeater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
                {
                    list.Add(s);
                }
            }
            return list;
        }


        #region 将字符串样式转换为纯字符串
        public static string GetCleanStyle(string StrList, string SplitString)
        {
            string RetrunValue = "";
            //如果为空，返回空值
            if (StrList == null)
            {
                RetrunValue = "";
            }
            else
            {
                //返回去掉分隔符
                string NewString = "";
                NewString = StrList.Replace(SplitString, "");
                RetrunValue = NewString;
            }
            return RetrunValue;
        }
        #endregion

        #region 将字符串转换为新样式
        public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
        {
            string ReturnValue = "";
            //如果输入空值，返回空，并给出错误提示
            if (StrList == null)
            {
                ReturnValue = "";
                Error = "请输入需要划分格式的字符串";
            }
            else
            {
                //检查传入的字符串长度和样式是否匹配,如果不匹配，则说明使用错误。给出错误信息并返回空值
                int strListLength = StrList.Length;
                int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
                if (strListLength != NewStyleLength)
                {
                    ReturnValue = "";
                    Error = "样式格式的长度与输入的字符长度不符，请重新输入";
                }
                else
                {
                    //检查新样式中分隔符的位置
                    string Lengstr = "";
                    for (int i = 0; i < NewStyle.Length; i++)
                    {
                        if (NewStyle.Substring(i, 1) == SplitString)
                        {
                            Lengstr = Lengstr + "," + i;
                        }
                    }
                    if (Lengstr != "")
                    {
                        Lengstr = Lengstr.Substring(1);
                    }
                    //将分隔符放在新样式中的位置
                    string[] str = Lengstr.Split(',');
                    foreach (string bb in str)
                    {
                        StrList = StrList.Insert(int.Parse(bb), SplitString);
                    }
                    //给出最后的结果
                    ReturnValue = StrList;
                    //因为是正常的输出，没有错误
                    Error = "";
                }
            }
            return ReturnValue;
        }
        #endregion

        //获取next
        public static int[] GetKmpNext(string pattern)
        {
            int[] next = new int[pattern.Length];
            next[0] = -1;
            if (pattern.Length < 2) return next;
            next[1] = 0;
            int i = 2, j = 0;
            while (i < pattern.Length)
            {
                if (pattern[i - 1] == pattern[j])
                {
                    next[i++] = ++j;
                }
                else
                {
                    j = next[j];
                    if (j == -1)
                    {
                        next[i++] = ++j;
                    }
                }
            }
            return next;
        }
        /// <summary>
        /// 查询关键字方法,传入源和关键字数组,多关键字数组中任意一个匹配即返回True
        /// </summary>
        /// <param name="source">原字符串</param>
        /// <param name="keywords">关键字列表用|分开</param>
        /// <returns>如果存在关键字返回true，反之返回false。</returns>
        public static bool SearchKeywords(string source, string[] keywords)
        {
            int wordCount = keywords.Length;
            int[][] nexts = new int[wordCount][];
            int i = 0;
            for (i = 0; i < wordCount; i++)
            {
                nexts[i] = GetKmpNext(keywords[i]);
            }
            i = 0;
            int[] j = new int[nexts.Length];
            while (i < source.Length)
            {
                for (int k = 0; k < wordCount; k++)
                {
                    if (source[i] == keywords[k][j[k]])
                    {
                        j[k]++;
                    }
                    else
                    {
                        j[k] = nexts[k][j[k]];
                        if (j[k] == -1)
                        {
                            j[k]++;
                        }
                    }
                    if (j[k] >= keywords[k].Length)
                    {
                        return true;
                    }
                }
                i++;
            }
            return false;
        }

        /// <summary>
        /// 是否为数字
        /// </summary>
        /// <param name="_string">字符串</param>
        /// <returns></returns>
        public static bool IsNumeric(string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            return _string.All(c => char.IsDigit(c));
        }

        /// <summary>
        /// 文件大小转换
        /// </summary>
        /// <param name="L"></param>
        /// <returns></returns>
        public static string GetSize(long L)
        {

            float result;

            var re = "";

            if (L >= 1073741824)
            {

                result = L / 1073741824.00F;

                re = "GB";

            }

            else if (L >= 1048576)
            {

                result = L / 1048576.00F;

                re = "MB";

            }

            else
            {

                result = L / 1024.00F;

                re = "KB";

            }

            string fileSize = result.ToString("0.00");

            return (fileSize + re);

        }

        #region 汉字拼音
        /// <summary>
        /// 取汉字首字母
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string GetChineseSpell(string strText)
        {
            int len = strText.Length;
            string myStr = "";
            for (int i = 0; i < len; i++)
            {
                myStr += getSpell(strText.Substring(i, 1));
            }
            return myStr;
        }

        private static string getSpell(string cnChar)
        {
            byte[] arrCN = Encoding.Default.GetBytes(cnChar);
            if (arrCN.Length > 1)
            {
                int area = (short)arrCN[0];
                int pos = (short)arrCN[1];
                int code = (area << 8) + pos;
                //{a..z}汉字拼音所在区位表的开始值，另撰文讲述GetBytes(char)所得值与汉字区位码的关系
                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };
                for (int i = 0; i < 26; i++)
                {
                    int max = 55290;
                    if (i != 25) max = areacode[i + 1];
                    if (areacode[i] <= code && code < max)
                    {
                        return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                    }
                }
                return "*";
            }
            else return cnChar;
        }

        #endregion
    }


}
